package sanphone.com.runningschool.PersonalInfo;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import sanphone.com.runningschool.BaseActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.R;

public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {
    private TextView nickNameText, realNameText, phoneText, dormText, idText;
    private String nickName, realName, phone, dorm;
    private String idStr;
    private SharedPreferences sprf;
    private CollapsingToolbarLayout collapsing_toolbar;
    private ImageView personInfo_background;
    private FloatingActionButton edit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);
        sprf = getSharedPreferences("data", MODE_PRIVATE);
        ForceOffline offline = new ForceOffline(sprf.getString("schoolId", null), this);
        offline.check();
        initView();
        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        idStr = pref.getString("schoolId", null);
        nickName = pref.getString("nickName", null);
        realName = pref.getString("userName", null);
        phone = pref.getString("userPhone", null);
        dorm = pref.getString("dormitory", null);

        setData();

    }

    private void setData() {
        idText.setText(idStr);
        nickNameText.setText(nickName);
        realNameText.setText(realName);
        phoneText.setText(phone);
        dormText.setText(dorm);
    }

    private void initView() {
        idText = findViewById(R.id.u_idText);
        nickNameText = findViewById(R.id.u_nickNameText);
        realNameText = findViewById(R.id.u_nametext);
        phoneText = findViewById(R.id.u_contact);
        dormText = findViewById(R.id.u_dorm_text);

        collapsing_toolbar = findViewById(R.id.collapsing_toolbar);
        collapsing_toolbar.setTitle("个人资料");
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        personInfo_background = findViewById(R.id.personInfo_background);
        Glide.with(this).load(R.drawable.p_background).into(personInfo_background);
        edit =  findViewById(R.id.edit);
        edit.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                PersonalInfoActivity.this.finish();
                break;
        }

        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit:
                startActivity(new Intent(PersonalInfoActivity.this, PersonalInfoEditActivity.class));
                PersonalInfoActivity.this.finish();
                break;

        }

    }
}
